package com.wy.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.ssm.domain.Product;
import com.wy.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: wy
 * @Date:2019/9/4 15:11
 */
//
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
       private ProductService productService;
         @RequestMapping("/findAll")
        public String findAllProduct(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "productName",required = false,defaultValue = "") String productName, Model model)throws Exception{

             List<Product> products = productService.findAll(pageNum,pageSize,productName);
             /*你推到其他页面所需参数*/
                 PageInfo<Product>  pageInfo= new PageInfo<>(products);

                   model.addAttribute("pageInfo",pageInfo);
                   return  "product-list";
         }


         @RequestMapping("/save")
         public String saveProduct(Product product) throws Exception{
            productService.save(product);
              return  "redirect:/product/findAll";
         }
}
